/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.maixiami;

import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.request.ItemcatsGetRequest;
import com.taobao.api.response.ItemcatsGetResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.apache.log4j.Logger;

/**
 *
 * @author qye
 */
public class ItemCategoryCrawler {
    static Logger logger = Logger.getLogger(ItemCategoryCrawler.class);
    
    public void crawl(String outfile) throws IOException, ApiException, InterruptedException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outfile));
        
        ItemcatsGetRequest req = new ItemcatsGetRequest();
        req.setFields("cid,parent_cid,name,is_parent");
        Queue<Long> parentIds = new LinkedList<Long>();
        parentIds.add(0L);
        List<ItemCat> itemCats = new ArrayList<ItemCat>();
        
        // bread first search
        while(!parentIds.isEmpty()) {
            req.setParentCid(parentIds.poll());
            ItemcatsGetResponse res = App.getTaobaoClient().execute(req);
            if(res.isSuccess()) {
                for(ItemCat cat : res.getItemCats()) {
                    itemCats.add(cat);
                    if(cat.getIsParent())
                        parentIds.add(cat.getCid());
                }
            }else {
                // retry once
                res = App.getTaobaoClient().execute(req);
                if(res.isSuccess()) {
                    for(ItemCat cat : res.getItemCats()) {
                        itemCats.add(cat);
                        if(cat.getIsParent())
                            parentIds.add(cat.getCid());
                    }
                }else {
                logger.warn(res.getMsg());
                }
            }
            Thread.sleep(100);
        }
        oos.writeObject(itemCats);
        oos.close();
    }
}
